import java.util.ArrayList;
import java.util.Scanner;

public class MainApp {
    //    键盘录入对象创建
    Scanner sc = new Scanner(System.in);
    //    创建User对象集合
    ArrayList<User> users = new ArrayList<>();
//    题库集合
    private static ArrayList<MathTest> mathTests = new ArrayList<>();
//        判断当前登录的对象身份
    String role1 = null;
   //程序入口
    public void start(){


        while(true) {
            System.out.println("====欢迎来到心算app平台====");
            System.out.println("请输入操作指令：");
            System.out.println("1.登录界面");
            System.out.println("2.注册界面");
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    //登录界面
                    userLogin();
                    break;
                case 2:
                    //注册界面
                    register();
                    break;
                default:
                    System.out.println("您的输入信息有误！请重新输入：");
            }
        }
    }
// 获取当前集合中的对象
    public User getUser(ArrayList<User> users,String name){
        User user=null;
        for (int i = 0; i < users.size(); i++) {
            User user1 = users.get(i);
            if (user1.getName().equals(name)){
                if (user1 instanceof Student){
                    Student student = (Student) user1;
                     user=student;
                }else if (user1 instanceof Teacher){
                    Teacher teacher = (Teacher) user1;
                     user=teacher;
                }
            }
        }
        return user;
    }

//    注册界面功能
    public  void register(){
        System.out.println("===欢迎来到注册界面===");
        while(true) {
            System.out.println("请选择注册角色：");
            String role = sc.next();
            switch (role) {
                case "学生":
                    User user1 = new Student();
                    System.out.println("请输入用户名：");
                    String name1 = sc.next();
                    user1.setName(name1);
                    while(true) {
                        System.out.println("请输入密码：");
                        String password1 = sc.next();
                        System.out.println("请再次输入密码：");
                        String password2 = sc.next();
                        if (password1.equals(password2)) {
                            user1.setPassword(password1);
                            break;
                        }else {
                            System.out.println("密码不一致，请重新输入：");
                        }
                    }
                    users.add(user1);//将录入的学生对象添加进集合
                    return;
                case "老师":
                    User user2 = new Teacher();
                    System.out.println("请输入用户名：");
                    String name2 = sc.next();
                    user2.setName(name2);
                    while(true) {
                        System.out.println("请输入密码：");
                        String password3 = sc.next();
                        System.out.println("请再次输入密码：");
                        String password4 = sc.next();
                        if (password3.equals(password4)){
                            user2.setPassword(password3);
                            break;
                        }else {
                            System.out.println("密码不一致，请重新输入：");
                        }
                    }
                    users.add(user2);//将录入的老师对象添加进集合
                    return;
                default:
                    System.out.println("输入有误！请重新输入：");
                    break;
            }
        }
    }
//    学生登录界面功能
    public void userLogin(){
        //判断当前集合中是否有用户对象
        if (users.size() == 0){
            System.out.println("当前系统无用户！");
            return;
        }
        System.out.println("===欢迎来到登录界面===");
        System.out.println("请输入您的名字确认身份：");
        String role = sc.next();
        User user1 = getUser(users, role);
        if (user1 == null) {
            System.out.println("当前无该对象！请退出重新注册！");
            start();
        }else if (user1 instanceof Student){
            role1="学生";
        }else {
            role1="老师";
        }
        System.out.println(role1+"您好！");
        while(true) {
            System.out.println("请输入用户名：");
            String loginName = sc.next();
            if (user1.getName().equals(loginName)) {
                break;
            } else {
                System.out.println("用户名输入错误！请重新输入：");
            }
        }
            while (true) {
                System.out.println("请输入密码：");
                String loginPassword = sc.next();
                if (user1.getPassword().equals(loginPassword)) {
                    break;
                } else {
                    System.out.println("密码错误！请重新输入：");
                }
            }
            System.out.println("登录成功！"+"欢迎"+role1+":"+user1.getName());
        if (role1.equals("老师")){
//            调用老师UI的方法
            showTeacherUI(user1);
        }else{
//            调用学生UI方法
            showStudentUI(user1);
        }
        }

//        学生UI功能方法
    public void showStudentUI(User user){
        System.out.println("欢迎"+user.getName()+"同学进入系统");
        System.out.println("您好，一共"+mathTests.size()+"道题目，请开始作答，答对一题得1分，答错不给分！");
        int fen = 0;
        for (int i = 0; i < mathTests.size(); i++) {
            System.out.println("请回答以下问题，机会只有一次：");
            MathTest mathTest = mathTests.get(i);
//            String str = mathTest.getQuestion().substring(0,6);
//            System.out.println(str);
            int trueAnswer = mathTest.calculate();//题目的真实答案
            if (mathTest instanceof DivideTest) {
                System.out.println("这是一道除法题，只要回答商就行，余数不考虑");
            }
            int answer = sc.nextInt();//用户输入的答案
            if (answer == trueAnswer){
                System.out.println("回答正确！");
                fen++;
            }else{
                System.out.println("回答错误！得0分！");
            }
        }
        System.out.println("一共"+mathTests.size()+"道题，每题一分，共得分"+fen);
        System.out.println("哇哦！算术这方面，你就是王者！佩服你");
        System.out.println("成绩不服，输入y继续挑战，否则输入其他退出！");
        String next = sc.next();
        switch (next){
            case "y":
                showStudentUI(user);
                break;
            default:
                return;
        }
    }



//        老师UI功能方法
public void showTeacherUI(User user){
        while(true) {
    System.out.println("欢迎进入"+user.getName()+role1+"的系统");
    System.out.println("当前题库如下：");
            for (int i = 0; i < mathTests.size(); i++) {
                MathTest mathTest = mathTests.get(i);
                String newStr="问题"+(i+1)+":"+mathTest.getQuestion();
                System.out.println(newStr);
            }
    System.out.println("是否要编辑题目，需要编辑输入y，输入其他无需修改，并退出登录!");
    String command = sc.next();
    switch (command){
        case "y":
            System.out.println("1.添加试题");
            System.out.println("2.删除试题：");
            System.out.println("请选择操作：");
            int num = sc.nextInt();
            if (num==1){
                addTest();
            }else if (num==2){
                deleteTest();
            }else {
                System.out.println("输入有误！请重新输入：");
            }
            break;
        default:
            return;
    }
    }
}

//   添加题目功能的方法
    public void addTest(){
        System.out.println("1.加法题");
        System.out.println("2.减法题");
        System.out.println("3.乘法题");
        System.out.println("4.除法题");
        System.out.println("请输入：");
        int num = sc.nextInt();
        switch (num){
            case 1:
                MathTest mathTest = new AddTest();//多态
                System.out.println("请输入被加数：");
                int jia1 = sc.nextInt();
                System.out.println("请输入加数：");
                int jia2 = sc.nextInt();
                mathTest.setA(jia1);
                mathTest.setB(jia2);
                mathTests.add(mathTest);
                System.out.println("题目"+jia1+"+"+jia2+"="+"?"+"\t"+"添加成功！");
                break;
            case 2:
                MathTest mathTest1 = new SubtractTest();//多态
                System.out.println("请输入被减数：");
                int jian1 = sc.nextInt();
                System.out.println("请输入减数：");
                int jian2 = sc.nextInt();
                mathTest1.setA(jian1);
                mathTest1.setB(jian2);
                mathTests.add(mathTest1);
                System.out.println("题目"+jian1+"-"+jian2+"="+"?"+"\t"+"添加成功！");
                break;
            case 3:
                MathTest mathTest2 = new MultiplyTest();
                System.out.println("请输入被乘数：");
                int cheng1 = sc.nextInt();
                mathTest2.setA(cheng1);
                System.out.println("请输入乘数：");
                int cheng2 = sc.nextInt();
                mathTest2.setB(cheng2);
                mathTests.add(mathTest2);
                System.out.println("题目"+cheng1+"*"+cheng2+"="+"?"+"\t"+"添加成功！");
                break;
            case 4:
                MathTest mathTest3 = new DivideTest();
                System.out.println("请输入被除数：");
                int chu1 = sc.nextInt();
                mathTest3.setA(chu1);
                System.out.println("请输入除数：");
                int chu2 = sc.nextInt();
                mathTest3.setB(chu2);
                mathTests.add(mathTest3);
                System.out.println("题目"+chu1+"/"+chu2+"="+"?"+"\t"+"添加成功！");
                break;
        }

    }

//删除试题功能的方法
    public void deleteTest(){
        System.out.println("请选择需要删除题目的题目号：");
        int num = sc.nextInt();
        mathTests.remove(num-1);
        System.out.println("删除第"+num+"题目"+"成功！");
    }

    }


